import socket
import time

import threading
import stopThreading

from UI.MainWindowUI import mainWindowUI


class UdpLogic(mainWindowUI):
    def __init_(self, num):
        super().__init__(num)
        self.udp_socket = None
        self.sever_th = None
        # self.client_th = None
        self.address = None
        self.client_socket_list = list()

    def udp_server_start(self):
        """
        开启UDP服务器方法
        :return:
        """
        self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        try:
            port = int(self.lineEdit_port.text())
            address = ("", port)
            self.udp_socket.bind(address)
        except Exception as e:
            msg = "请检查端口号\n"
            self.signal_write_msg.emit(msg)
        else:
            self.sever_th = threading.Thread(target=self.udp_server_concurrency)
            self.sever_th.start()
            msg = "UDP服务端正在监听端口:{}\n".format(port)
            self.signal_write_msg.emit(msg)

    def udp_server_concurrency(self):
        """
        用于创建一个线程持续监听UDP通信
        :return: None
        """
        while True:
            recv_msg, recv_addr = self.udp_socket.recvfrom(1024)
            msg = recv_msg.decode("utf-8")
            msg = "来自IP:{}端口:{}:\n{}\n".format(recv_addr[0], recv_addr[1], msg)
            self.signal_write_msg.emit(msg)

    def udp_client_start(self):
        """
        确认UDP客户端的ip及地址
        :return:
        """
        self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        try:
            self.address = (
                str(self.lineEdit_ip_send.text()),
                int(self.lineEdit_port.text()),
            )
        except Exception as e:
            msg = "请检查端口号\n"
            self.signal_write_msg.emit(msg)
        else:
            msg = "UDP客户端已启动\n"
            self.signal_write_msg.emit(msg)

    def udp_send(self):
        """
        功能函数，用于UDP客户端发送消息
        :return: None
        """
        if self.link is False:
            msg = "请选择服务，并点击连接网络\n"
            self.signal_write_msg.emit(msg)
        else:
            try:
                send_msg = (str(self.textEdit_send.toPlainText())).encode("utf-8")
                if self.comboBox_tcp.currentIndex() == 2:
                    msg = "UDP服务端无法发送，请切换为UDP客户端\n"
                    self.signal_write_msg.emit(msg)
                if self.comboBox_tcp.currentIndex() == 3:
                    self.udp_socket.sendto(send_msg, self.address)
                    msg = "UDP客户端已发送\n"
                    self.signal_write_msg.emit(msg)
            except Exception as ret:
                msg = "发送失败\n"
                self.signal_write_msg.emit(msg)

    def udp_close(self):
        """
        功能函数，关闭网络连接的方法
        :return:
        """
        if self.comboBox_tcp.currentIndex() == 2:
            try:
                self.udp_socket.close()
                if self.link is True:
                    msg = "已断开网络\n"
                    self.signal_write_msg.emit(msg)
            except Exception as ret:
                pass
        if self.comboBox_tcp.currentIndex() == 3:
            try:
                self.udp_socket.close()
                if self.link is True:
                    msg = "已断开网络\n"
                    self.signal_write_msg.emit(msg)
            except Exception as ret:
                pass
        try:
            stopThreading.stop_thread(self.sever_th)
        except Exception:
            pass
        try:
            stopThreading.stop_thread(self.client_th)
        except Exception:
            pass
